package org.grayrabbit.cms.publish.generate.factory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Category;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.publish.generate.CategoryListGenerater;
import org.grayrabbit.cms.publish.generate.Generater;
import org.grayrabbit.cms.publish.generate.MultiGenerater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import freemarker.template.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @created 15-����-2011 10:43:13
 */
public class CategoryListGeneraterFactory implements GeneraterFactory {
	private Logger LOG = LoggerFactory.getLogger(CategoryListGeneraterFactory.class);
	private Configuration configuration;
	private List<Category> categorys = new ArrayList<Category>();
	private Map<String,List<Content>> contents;
	private String rootPath;
	private String[] parentPath;
	private int pageSize;
	private int pageNum;
	private Generatable generatable;
	private Admin admin;

	public CategoryListGeneraterFactory(Configuration configuration, List<Category> categorys, String rootPath,String[] parentPath,Map<String,List<Content>> contents,int pageSize,int pageNum,Generatable generatable,Admin admin){
		this.configuration = configuration;
		this.categorys = categorys;
		this.rootPath = rootPath;
		this.parentPath = parentPath;
		this.contents = contents;
		this.pageSize = pageSize;
		this.pageNum = pageNum;
		this.generatable = generatable;
		this.admin = admin;
	}
	
	public CategoryListGeneraterFactory(Configuration configuration, Category category, String rootPath,String[] parentPath,Generatable generatable,Admin admin){
		this.configuration = configuration;
		this.categorys.add(category);
		this.rootPath = rootPath;
		this.parentPath = parentPath;
		this.generatable = generatable;
		this.admin = admin;
	}

	public Generater create(){
		List<Generater> catlist = new ArrayList<Generater>();
		int j = 0;
		for (Category category : categorys) {
			List<Generater> list = new ArrayList<Generater>();
			String path = category.getCatContentListTpl().substring((File.separator+rootPath).length());
			if(contents.get(category.getCategoryId())!=null){
				List<Content> cList = new ArrayList<Content>();
				
				for(Content obj : contents.get(category.getCategoryId())){
					obj.setCategory(category);
					cList.add(obj);
				}
				int maxPage = (contents.get(category.getCategoryId()).size()%pageSize==0)?(contents.get(category.getCategoryId()).size()/pageSize):(contents.get(category.getCategoryId()).size()/pageSize+1);
				if(maxPage>1){
					for(int i = 0;i<maxPage;i++){
						List<Content> rs =  getSubList(cList,i+1);
						if(rs!=null){
							list.add(new CategoryListGenerater(configuration,path,category,null,i+1,maxPage,parentPath[j],rs,generatable,admin));
						}
					}
				}else{
					list.add(new CategoryListGenerater(configuration,path,category,null,1,maxPage,parentPath[j],cList,generatable,admin));
					LOG.info("\n *******************"+ contents.get(category.getCategoryId()).size());
				}
			}else{
				list.add(new CategoryListGenerater(configuration,path,category,null,pageNum,pageNum,parentPath[j],null,generatable,admin));
			}
			j++;
			MultiGenerater mg = new MultiGenerater(list);
			mg.setNeedAddFinised(true);
			mg.setGeneratable(generatable);
			catlist.add(mg);
		}
		if(catlist.size()==1)
			return catlist.get(0);
		return new MultiGenerater(catlist);
	}
	
	private List<Content> getSubList(List<Content> list,int page){
		int size = list.size();
		if(size<=pageSize){
			return list;
		}
		else if((page*pageSize)>(size+pageSize)){
			return null;
		}
		else{
			int i =page*pageSize>size?size:page*pageSize;
			List<Content> obj = new ArrayList<Content>(list.subList((page-1)*pageSize, i));
			return obj;
		}
	}

	
}